home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 2.toast / pc / sample code / sound / audiocodec / ulawcodec.h < prev    next >
Encoding:
Text File  |  2000-09-28  |  6.3 KB  |  186 lines

  1. // common header file shared by both the code and resource compilers
  2.  
  3.  
  4. /*
  5.     How this works:
  6.  
  7.     In uLaw, 14-bits of linear sampling is reduced to 8 bits of logarithmic data.
  8.     It is used on North American and Japanese phone systems, and is coming into use
  9.     for voice data interchange, for PBXs, voicemail, MIME (Internet standard for multimedia mail),
  10.     and Internet Talk Radio. µLaw sounds are almost always encoded at 8000, 8012, or 8012.8210513
  11.     samples/sec. 8000 because it is the specified standard for phones, and 8012.8210513
  12.     (and rounded to 8012) because it is apparently the actual rate used in domestic digital phone switches.
  13.  
  14.     ---------------------------
  15.     U-LAW and A-LAW definitions
  16.     ---------------------------
  17.  
  18.     [Adapted from information provided by duggan@cc.gatech.edu (Rick
  19.     Duggan) and davep@zenobia.phys.unsw.EDU.AU (David Perry)]
  20.  
  21.     u-LAW (really mu-LAW) is
  22.  
  23.               sgn(m)   (     |m |)       |m |
  24.        y=    ------- ln( 1+ u|--|)       |--| =< 1
  25.              ln(1+u)   (     |mp|)       |mp|
  26.  
  27.     A-LAW is
  28.  
  29.          |     A    (m )                 |m |    1
  30.          |  ------- (--)                 |--| =< -
  31.          |  1+ln A  (mp)                 |mp|    A
  32.        y=|
  33.          | sgn(m) (        |m |)    1    |m |
  34.          | ------ ( 1+ ln A|--|)    - =< |--| =< 1
  35.          | 1+ln A (        |mp|)    A    |mp|
  36.  
  37.     Values of u=100 and 255, A=87.6, mp is the Peak message value, m is the current quantised message value.
  38.     (The formulae get simpler if you substitute x for m/mp and sgn(x) for sgn(m); then -1 <= x <= 1.)
  39.  
  40.     Converting from u-LAW to A-LAW is in a sense "lossy" since there are quantizing errors introduced in
  41.     the conversion.
  42.  
  43.     "..the u-LAW used in North America and Japan, and the A-LAW used in Europe and the rest of the world
  44.     and international routes.."
  45.  
  46.     References:
  47.  
  48.     Modern Digital and Analog Communication Systems, B.P.Lathi., 2nd ed. ISBN 0-03-027933-X
  49.  
  50.     Transmission Systems for Communications
  51.     Fifth Edition
  52.     by Members of the Technical Staff at Bell Telephone Laboratories
  53.     Bell Telephone Laboratories, Incorporated
  54.     Copyright 1959, 1964, 1970, 1982
  55. */
  56.  
  57. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  58. // resource IDs
  59.  
  60. #define kSoundDecompressorResID            1000        // ID of decompressor 'thng', code resources, and table
  61. #define kSoundCompressorResID            2000        // ID of compressor 'thng', code resources, and table
  62. #define kSoundCodecNameStringResID        1000        // ID of codec name
  63. #define kSoundCodecInfoStringResID        1001        // ID of codec info string
  64. #define kSoundComponentIconResID        1000        // ID of codec icon
  65.  
  66. #define kSoundComponentManufacturer        '????'        // your company's OSType
  67. #define kCodecFormat                    kULawCompression
  68.  
  69.  
  70. #define kSoundDecompressorVersion        0x00050000    // Use a higher version to overide the default component
  71. #define kSoundCompressorVersion            0x00050000    // Use a higher version to overide the default component
  72.  
  73. #define k16BitTableResType                'Wtab'        // resource type for a 16 bit array
  74. #define k8BitTableResType                'Btab'        // resource type for a 8 bit array
  75.  
  76.  
  77. // resource names will be created for debug versions
  78. #ifdef _DEBUG
  79. #define ULawDecompressorName            "µLaw Decompressor"
  80. #define ULawCompressorName                "µLaw Compressor"
  81. #else
  82. #define ULawDecompressorName            ""
  83. #define ULawCompressorName                ""
  84. #endif
  85.  
  86.  
  87. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  88. // code constants
  89.  
  90. #define kMaxOutputSamples        1024        /* approximate no. samples in output buffer */
  91.  
  92. #define kULawBlockSamples        1            /* no. samples in a block */
  93. #define kULawBlockBytes            1            /* no. bytes in a block */
  94. #define kULawBytesPerSample        2            /* no. bytes in decompressed sample */
  95.  
  96.  
  97. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  98. // failure handling macros
  99. /*
  100.     Some macros used to check for errors and also to allow for
  101.     handling them by using a goto statement.  This makes the source
  102.     code easier to read.  It will break into the debugger with a
  103.     message showing the condition that caused the failure.  In some
  104.     of the macros the debug message is removed but goto remains.  In
  105.     other macros all of it is removed when doing a non-debug build.
  106.  
  107.     Note that these macros use the "\p" construct for creating
  108.     Pascal strings at compile time.  Most non-Mac compilers do
  109.     not recognize this, give a warning, and put 'p' as the first
  110.     character of the string.  You can ignore the warning because
  111.     the non-Mac version of DebugStr deals with this just fine.
  112.     For Microsoft's Visual C++, we suppress the warning below.
  113. */
  114. #if defined(_MSC_VER) && !defined(__MWERKS__) 
  115.     // Visual C++ from Microsoft
  116.     #pragma warning(disable:4129) // unrecognized character escape sequence
  117. #endif
  118.  
  119. // This checks for the exception, and if true then goto handler
  120.  
  121. #ifdef _DEBUG
  122. #define FailIf(cond, handler)                                \
  123.     if (cond) {                                                \
  124.         DebugStr((ConstStr255Param)"\p"#cond " goto " #handler);    \
  125.         goto handler;                                        \
  126.     } else 0
  127. #else
  128. #define FailIf(cond, handler)                                \
  129.     if (cond) {                                                \
  130.         goto handler;                                        \
  131.     } else 0
  132. #endif
  133.  
  134. // This checks for the exception, and if true do the action and goto handler
  135.  
  136. #ifdef _DEBUG
  137. #define FailWithAction(cond, action, handler)                \
  138.     if (cond) {                                                \
  139.         DebugStr((ConstStr255Param)"\p"#cond " goto " #handler);    \
  140.         { action; }                                            \
  141.         goto handler;                                        \
  142.     } else 0
  143. #else
  144. #define FailWithAction(cond, action, handler)                \
  145.     if (cond) {                                                \
  146.         { action; }                                            \
  147.         goto handler;                                        \
  148.     } else 0
  149. #endif
  150.  
  151. // This will insert debugging code in the application to check conditions
  152. // and displays the condition in the debugger if true.  This code is
  153. // completely removed in non-debug builds.
  154.  
  155. #ifdef _DEBUG
  156. #define FailMessage(cond)                                    \
  157.     if (cond)                                                \
  158.         DebugStr((ConstStr255Param)"\p"#cond);                \
  159.     else 0
  160. #else
  161. #define FailMessage(cond)
  162. #endif
  163.  
  164. // This allows you to test for the result of a condition (i.e. CloseComponent)
  165. // and break if it returns a non zero result, otherwise it ignores the result.
  166. // When a non-debug build is done, the result is ignored.
  167.  
  168. #ifdef _DEBUG
  169. #define ErrorMessage(cond)                                    \
  170.     if (cond)                                                \
  171.         DebugStr((ConstStr255Param)"\p"#cond);                \
  172.     else 0
  173. #else
  174. #define ErrorMessage(cond)        cond
  175. #endif
  176.  
  177. // This will display a given message in the debugger, this code is completely
  178. // removed in non-debug builds.
  179.  
  180. #ifdef _DEBUG
  181. #define DebugMessage(s)            DebugString((ConstStr255Param)s)
  182. #else
  183. #define DebugMessage(s)
  184. #endif
  185.  
  186.